var m302root = document.getElementById("m302root");
var currentCount = 1;

mui('body').on('tap','#m302btnadd',function(){
	//卡片初始化
	var card = cardInit();
	
	//卡片头初始化
	var chead = cardHeadInit();
	chead.appendChild(labelInit(currentCount+'.'));
	chead.appendChild(inputInit("请输入题目",titleId(currentCount)));
	//chead.appendChild(spanCloseEmpty());
	
	//卡尾初始化
	var cfoot = cardFootInit();
	
	//div1 Part 添加三个Radio按钮，并将div1添加到卡尾巴中
	var div1 = divInit();
	var radio1 = radioInit("单选",currentCount,"A");
	var radio2 = radioInit("多选",currentCount,"B");
	var radio3 = radioInit("简答",currentCount,"C");
	div1.appendChild(radio1);
	div1.appendChild(radio2);
	div1.appendChild(radio3);
	cfoot.appendChild(div1);
	
	//初始化div2 Part 用来响应Part1初始化后的Radio的OnChange事件
	var div2 = divInitWithId(div2Id(currentCount));
	
	//将div2添加到卡片尾巴中取
	cfoot.appendChild(div2);
	
	//将卡片头和卡片尾添加到卡片中
	card.appendChild(chead);
	card.appendChild(cfoot);
	
	//将卡片添加到主HTML中
	m302root.appendChild(card);
	
	//i++更新
	currentCount++;
});



function cardInit()
{
	var card = document.createElement("div");
	card.setAttribute("class","mui-card");
	card.setAttribute("id","card");
	return card;
}
function cardHeadInit()
{
	var head = document.createElement("div");
	head.setAttribute("class","mui-card-header");
	return head;
}
function cardFootInit()
{
	var foot = document.createElement("div");
	foot.setAttribute("class","mui-card-footer");
	return foot;
}
function labelInit(value)
{
	var label = document.createElement("label");
	label.innerHTML=value;
	return label;
}
function inputInit(hint,id)
{
	var input = document.createElement("input");
	input.setAttribute("id",id);
	console.log(hint+":"+id);
	input.setAttribute("type","text");
	input.setAttribute("placeholder",hint);
	return input;
}
function divInit()
{
	return document.createElement("div");
}
function divInitWithId(id)
{
	var div = document.createElement("div");
	div.setAttribute("id",id);
	return div;
}
function spanCloseEmpty(cardid)
{
	var span = document.createElement("span");
	span.setAttribute("id",cardid);
	span.setAttribute("class","mui-icon mui-icon-closeempty");
	return span;
}

function radioInit(labels,name,type)
{
	var radio = document.createElement("div");
	radio.setAttribute("class","mui-input-row mui-radio");
	var label = document.createElement("label");
	label.innerHTML = labels;
	radio.appendChild(label);
	var input = document.createElement("input");
	input.setAttribute("name",name);
	input.setAttribute("type","radio");
	input.setAttribute("value",type);
	input.setAttribute("onchange","radioChangeCallback('D"+name+"','"+type+"','"+name+"')");
	radio.appendChild(input);
	return radio;
}
function radioChangeCallback(val,type,nums){
	var div = document.getElementById(val);
	div.innerHTML="";
	if(type=='C')
		return;
	var divs = divInitWithId("D"+val);
	var inputA = inputInit("选项A",choice1Id(nums));
	var inputB = inputInit("选项B",choice2Id(nums));
	divs.appendChild(inputA);
	divs.appendChild(inputB);
	div.appendChild(divs);
	var btn = btnInit("D"+val,nums);
	div.appendChild(btn);
}
function btnInit(val,nums){
	var btn = document.createElement("button");
	btn.innerHTML = "添加选项";
	btn.setAttribute("class","mui-btn mui-btn-primary mui-btn-block");
	btn.setAttribute("onclick","genCDInput('"+val+"','"+nums+"')");
	return btn;
}
function genCDInput(val,nums){
	var div = document.getElementById(val);
	if(div.childNodes.length==4)
	{
		mui.toast("目前最多支持四个选项哦。")
		return;
	}
	if(div.childNodes.length==2)
	{
		div.appendChild(inputInit("选项C",choice3Id(nums)));
	}else
	{
		div.appendChild(inputInit("选项D",choice4Id(nums)));
	}
}

mui('body').on('tap','#m302btnsub',function(){
	var uid = localStorage.getItem('uid');
	if(uid==null||uid=='')
	{
		mui.toast("请先登录后再进行此操作");
		return;
	}
	

	var body = document.getElementById("m302r0").value;
	var level = getVals("level");
	var live = document.getElementById("live").value;
	
	if(body=='')
	{
		mui.toast("您还没有输入您的标题哦");
		return;
	}
	if(live=='')
	{
		mui.toast("您还没有输入问卷生存事件哦");
		return;
	}
	
	var cards = document.getElementsByClassName("mui-card");
	
	var empty = {};
	for(j=1;j<=cards.length-2;j++)
	{
		var rds = getVals(j);
		if(rds==null)
		{
			mui.toast("您第"+j+"题未完善哦！");
			return;
		}
		var sbody = document.getElementById(titleId(j)).value;
		if(rds!='C')
		{
			var div2 = document.getElementById("DD"+j);
			var k = div2.childNodes.length-2;
			var aval = document.getElementById(choice1Id(j)).value;
			var bval = document.getElementById(choice2Id(j)).value;
			if(k==0){
				empty[j] = {"type":rds,"body":sbody,"A":aval,"B":bval,"choices":2};
			}else if(k==1){
				var cval = document.getElementById(choice3Id(j)).value;
				empty[j] = {"type":rds,"body":sbody,"A":aval,"B":bval,"C":cval,"choices":3};
			}else{
				var cval = document.getElementById(choice3Id(j)).value;
				var dval = document.getElementById(choice4Id(j)).value;
				empty[j] = {"type":rds,"body":sbody,"A":aval,"B":bval,"C":cval,"D":dval,"choices":4};
			}
		}else{
			empty[j]={"type":rds,"body":sbody};
		}
		
	}
	
	var edata = {
		'uid'  :uid,
		'body' :body,
		'nums' :cards.length,
		'level':level,
		'live' :live,
		'data' :empty,
	}
	console.log(edata);
	
	mui.confirm('请问现在需要提交问卷吗？','提示：',new Array('否，再等一下','是，现在提交'),function(e){
		if(e.index==1) {
			m302ajax(edata);
		}
	});
	
});
function getVals(name)
{
	var rdsObj = document.getElementsByName(name);
	for(n=0;n<rdsObj.length;n++)
	{
		if(rdsObj[n].checked) return rdsObj[n].value;
	}
	return null;
}

function m302ajax(edata)
{
	mui.ajax(ASYURI+'A1012.do',{
		data:edata,
		dataType:'json',
		type:'post',
		timeout:10000,
		success : function(data){
			if(data.tag=='0')
			{
				mui.toast("您已经成功提交了您的问卷");
				mui.back();
			}
			else if(data.tag=='2')
			{
				mui.toast("提交问卷出现了错误");
			}
		},
		error : function(xhr,type,errorThrown){
			console.log(xhr+":"+type+"-"+errorThrown);
		}
	});
}
//ID Block
function titleId(idnums)
{
	return "T"+idnums;
}
function choice1Id(idnums)
{
	return "CA" + idnums;
}
function choice2Id(idnums)
{
	return "CB" + idnums;
}
function choice3Id(idnums)
{
	return "CC" + idnums;
}
function choice4Id(idnums)
{
	return "CD" + idnums;
}
function div2Id(idnums)
{
	return "D" + idnums;
}

function initFirst(){
	
	var div0 = document.getElementById("div0");
	mui.ajax(ASYURI+'B1013.do',{
		data:{
			'uid':localStorage.getItem('uid'),
		},
		dataType:'json',//服务器返回json格式数据
		type:'post',//HTTP请求类型
		timeout:10000,//超时时间设置为10秒；
		success:function(data){
			if(data.tag!='-1'){
				var subdiv = document.createElement("div");
				subdiv.setAttribute("class","mui-input-row mui-radio");
				subdiv.appendChild(labelInit("仅限组内"))
				var subinput = document.createElement("input");
				subinput.setAttribute("name","level");
				subinput.setAttribute("type","radio");
				subinput.setAttribute("value","-1");
				subdiv.appendChild(subinput);
				div0.appendChild(subdiv);
			}
		},
		error:function(xhr,type,errorThrown){
			
		}
	});
	
	
	//卡片初始化
	var card = cardInit();
	
	//卡片头初始化
	var chead = cardHeadInit();
	var titleLabel = labelInit(currentCount+'.');
	var titleInput = inputInit("请输入题目",titleId(currentCount));
	chead.appendChild(titleLabel);
	chead.appendChild(titleInput);
	
	//卡尾初始化
	var cfoot = cardFootInit();
	
	//div1 Part 添加三个Radio按钮，并将div1添加到卡尾巴中
	var div1 = divInit();
	var radio1 = radioInit("单选",currentCount,"A");
	var radio2 = radioInit("多选",currentCount,"B");
	var radio3 = radioInit("简答",currentCount,"C");
	div1.appendChild(radio1);
	div1.appendChild(radio2);
	div1.appendChild(radio3);
	cfoot.appendChild(div1);
	
	//初始化div2 Part 用来响应Part1初始化后的Radio的OnChange事件
	var div2 = divInitWithId(div2Id(currentCount));
	
	//将div2添加到卡片尾巴中取
	cfoot.appendChild(div2);
	
	//将卡片头和卡片尾添加到卡片中
	card.appendChild(chead);
	card.appendChild(cfoot);
	
	//将卡片添加到主HTML中
	m302root.appendChild(card);
	
	//i++更新
	currentCount++;
}

initFirst();